/*
 * 百度在线网络技术（北京）有限公司拥有本软件版权2021并保留所有权利。
 * Copyright 2021, Baidu.com,Inc 2:Baidu Online Network Technology (Beijing) Co.,Ltd,
 * All rights reserved.
 */

package com.azdebugit.concurrent;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
class Writer extends Thread {
    private CyclicBarrier cbr;
    public Writer(CyclicBarrier cbr) {
        this.cbr = cbr;
    }
    @Override
    public void run() {
        try {
            System.out.println("线程" + Thread.currentThread().getName() + ",正在写入数据");
            Thread.sleep(3000);
            System.out.println("线程" + Thread.currentThread().getName() + ",写入数据成功.....");
            cbr.await();
            System.out.println("所有线程执行完毕..........");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
public class CyclicBarrierTest {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
        for (int i = 0; i < 5; i++) {
            Writer writer = new Writer(cyclicBarrier);
            writer.start();
        }
    }
}